<?php
/*
 * 主要功能: 分頁,,有問題問我吧,沒時間寫注
 *
 * dreamxyp(QQ:31996798) - PHP - class.pg.php
 * coding:夏一平
 * 創建時間:2006-10-30
 */
class pg
{
    /*
     * 功能:取得本頁文件名
     * 參數:無
     * 返回:string 文件名
     */
    static public function self()
    {
        return $_SERVER['PHP_SELF'];
    }
    /*
     * 功能:得到URL數據
     */
    static public function pageData($ignore= array(), $append= array (), $postMaxLen= 50)
    {
        $rs= array ();
        !is_array($ignore) && $ignore= explode(',', $ignore);
		$arr = array_merge($_POST,$_GET,$append);
        foreach ($ignore as  &$v) unset($arr[$v]);       
        foreach ($arr as $k => &$v)
        {
            if ($v && strlen($v) < $postMaxLen) $rs[$k]= $v;
        }
        return $rs;
    }
    /*
     * 生成完整URL
     * 參數:$data	get數據
     * 返回:string	URL
     */
    static public function url($ignore= array (), $extend= null, $page= false, $append= array (), $postMaxLen= 50)
    {
        $mark= false;
        !$page && $page= self :: self();
        $urlDate= self :: pageData($ignore, $append, $postMaxLen);
		$arr = array();
        foreach ($urlDate as $k => & $v)$arr[] = "{$k}=" . urlencode($v);
        $extend && $arr[] = "{$extend}=";
        return $page."?".implode("&",$arr);
    }
    static public function psData($db,$table,$where=array(),$page= 1,$rows= 10,$max= 5)
    {
        $page = $page < 1?1 : $page;
		$page = (int)$page;
		
		$sub['t']= $max;
		$sub['m']= ceil($max / 2);
		if (is_numeric($db))
		{
			$rCount= (int)$db;
		}else
		{
			$rsArray= $db->rsArray("select count(*) as rCount from {$table} {$where['where']}", $where['bind']);
			$rCount= $rsArray['rCount'];
		}
		$pageCount = ceil($rCount / $rows);
		
		$returns['all'] 	= $rCount;
		$returns['pc']  	= $pageCount;
		$returns['begin']	= $rows * ($page -1);
		$returns['this']	= $page;
		$returns['np']		= array ();
		if ($pageCount > $sub['t'])
		{
			$sub['C']= $sub['t'];
			$sub['Begin']= true;
			$sub['End']= true;
			if ($returns['this'] <= $sub['m'])
			{
				$sub['Start']= 1;
				$sub['Begin']= false;
			}elseif (($pageCount - $returns['this']) < $sub['m'])
			{
				$sub['Start']= $pageCount - $sub['t'] +1;
				$sub['End']= false;
			}else
			{
				$sub['Start']= $returns['this'] - $sub['m'] + 1;
			}
		}else
		{
			$sub['C']= $pageCount; //
			$sub['Begin']= false;
			$sub['End']= false;
			$sub['Start']= 1;
		}
		$sub['Next']= $returns['this'] != $pageCount && $pageCount > 1 ? true : false;
		$sub['Previous']= $returns['this'] != 1 && $pageCount > 1 ? true : false;
		
		$sub['Begin'] && $returns['np'][]= array ('Begin' => 1);
		$sub['Previous'] && $returns['np'][]= array ('Previous' => $page -1);
		for ($i= $sub['Start']; $i < ($sub['Start'] + $sub['C']); $i++)$returns['np'][]= array ($i => $i);
		$sub['Next'] && $returns['np'][]= array (	'Next' => $page +1);
		$sub['End'] && $returns['np'][]= array ('End' => $pageCount);
		
        unset ($sub);
        return $returns;
    }
    /*
     * 功能:分頁
     */
    static public function sp($db,$table,$where=array(),$page= 1,$rows= 10,$url=null,$max= 9)
    {
        empty ($url) && $url= self :: url(array('page'), 'page');        
        $returns= self :: psData($db,$table,$where,$page,$rows,$max);
        $returns['str']= "總共有{$returns['all']}個,共{$returns['pc']}頁,第{$returns['this']}頁";
        $returns['input']= "轉到<script type=\"text/javascript\">
		var __php_input = function(event,a)
		{
			if(event.keyCode==13)
			{
				if(a.value<={$returns['pc']})
				{
					document.location = '{$url}'+a.value;
				}else
				{ 
					alert('數值太大!');
				}
			};
			if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;
		}
		var __php_change = function(a)
		{
			if(a.value<={$returns['pc']})
			{
				document.location = '{$url}'+a.value;
			}else
			{ 
				alert('數值太大!');
			}
		}
		</script>
		<input onKeypress=\"__php_input(event,this)\" type=\"text\" value=\"{$page}\" size=\"2\" maxlength=\"5\"  style=\"ime-mode:disabled\" onChange=\"__php_change(this)\" />頁";
        foreach ($returns['np'] as &$v)
        {
            if ($v['Begin'])
            {
                $v= '<li><a href="' . $url . $v['Begin'] . '" title="轉到第一頁">&lt;&lt;</a></li>';
            }
            elseif ($v['Previous'])
            {
                $v= '<li><a href="' . $url . $v['Previous'] . '" title="上一頁">&lt;</a></li>';
            }
            elseif ($v['Next'])
            {
                $v= '<li><a href="' . $url . $v['Next'] . '" title="下一頁">&gt;</a></li>';
            }
            elseif ($v['End'])
            {
                $v= '<li><a href="' . $url . $v['End'] . '" title="轉到最後一頁">&gt;&gt;</a></li>';
            }
            elseif ($v['this'])
            {
                $v= '<li><a href="' . $url . $v['this'] . '" title="本頁" onclick="return false">' . $returns['this'] . '</a></li>';
            }
            else
            {
                $tmp= $v;
                foreach ($tmp as $key => &$value)
                {
                    if ($returns['this'] == $key)
                    {
                        $v= '<li class="now"><a href="' . $url . $value . '" title="本頁" onclick="return false">' . $key . '</a></li>';
                    }
                    else
                    {
                        $v= '<li><a href="' . $url . $value . '">' . $key . '</a></li>';
                    }
                }
            }
        }
        $returns['np'] = array_reverse($returns['np'], TRUE);
        return $returns;
    }
}
?>